package com.css.openapi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.css.common.core.constant.SystemCodeEnum;
import com.css.common.core.exception.ServiceException;
import com.css.common.core.utils.GyUtils;
import com.css.openapi.BO.*;
import com.css.openapi.BO.lypt.SqmResponse;
import com.css.openapi.BO.lypt.ZfjkpYlwebQydglWxtjrResponse;
import com.css.openapi.entity.QyYwlx;
import com.css.openapi.feign.ZfjkpPtCommonClient;
import com.css.openapi.mapper.QyYwlxMapper;
import com.css.openapi.service.LqsqWxtjrService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.*;

/**
 * <p>
 * 乐企授权服务实现类(外系统接入)
 * </p>
 *
 * @author zhuowugong
 * @since 2024-07-29 09:58:16
 */
@Service
@Slf4j
public class LqsqWxtjrServiceImpl implements LqsqWxtjrService {
 @Resource
 private QyYwlxMapper qyYwlxMapper;
    @Resource
    private ZfjkpPtCommonClient zfjkpPtCommonClient;
    /**
     * 通过getSqm获取二维码后，有效期10分钟，期间前端通过定时任务再调用getSqmValidResult，若该接口出参code不为0说明未成功则继续调用该接口，
     * 若为0说明成功，则取消定时任务，最后调用getSqmValidSuccessBdJg，若该接口出参code为0则提示操作成功，否则提示操作失败。
     */
    @Override
    public SqmVO getSqm(LqsqWxtjrReq param) {
        if (GyUtils.isNull(param.getShbdbh()))
            throw new ServiceException(SystemCodeEnum.SYSTEM_NO_VALID.getCode(), "获取不到商户绑定编号");
        QyYwlx qyYwlx=qyYwlxMapper.getQyBdzt(param.getShbdbh());
        if (Objects.isNull(qyYwlx))
            throw new ServiceException(SystemCodeEnum.SYSTEM_NO_VALID.getCode(), "未查询到此商户绑定编号：" + param.getShbdbh() + "的信息");
        String qysh = qyYwlx.getShsh();
        log.info("外系统接入获取授权码参数:{}，qyjr:{}", JSON.toJSONString(param), JSON.toJSONString(qyYwlx));
        if (GyUtils.isNull(qysh)) throw new ServiceException(SystemCodeEnum.SYSTEM_NO_VALID.getCode(), "获取不到当前企业税号");
        final String sqrType = param.getSqrlx();
        if (GyUtils.isNull(sqrType)) throw new ServiceException(SystemCodeEnum.SYSTEM_NO_VALID.getCode(), "授权人类型不能为空");
        final String sqrName = param.getXm();
        if (GyUtils.isNull(sqrName)) throw new ServiceException(SystemCodeEnum.SYSTEM_NO_VALID.getCode(), "授权人姓名不能为空");
        final String sqrIdCardType = param.getSqrIdCardType();
        if (GyUtils.isNull(sqrIdCardType))
            throw new ServiceException(SystemCodeEnum.SYSTEM_NO_VALID.getCode(), "授权人身份证件类型不能为空");
        final String sqrIdCard = param.getJmsfzhm();
        if (GyUtils.isNull(sqrIdCard))
            throw new ServiceException(SystemCodeEnum.SYSTEM_NO_VALID.getCode(), "授权人身份证件号码不能为空");
        SqmVO sqmVO = new SqmVO();
        SqmResponse sqmResponse= zfjkpPtCommonClient.getSqm(param);
        if(!Objects.equals("0",sqmResponse.getCode()))throw new ServiceException(sqmResponse.getMsg());
        BeanUtil.copyProperties(sqmResponse,sqmVO,false);
        return sqmVO;
    }


    @Override
    public YlwebQydglWxtjrResponse getShlqsqzt(LqsqWxtjrReq param) {
        if (GyUtils.isNull(param.getShbdbh()))
            throw new ServiceException(SystemCodeEnum.SYSTEM_NO_VALID.getCode(), "获取不到商户绑定编号");
        QyYwlx qyYwlx=qyYwlxMapper.getQyBdzt(param.getShbdbh());
        if (Objects.isNull(qyYwlx))
            throw new ServiceException(SystemCodeEnum.SYSTEM_NO_VALID.getCode(), "未查询到此商户绑定编号：" + param.getShbdbh() + "的信息");
        ZfjkpYlwebQydglWxtjrResponse response= zfjkpPtCommonClient.getShlqsqzt(param);
        if(!Objects.equals("0",response.getCode()))throw new ServiceException(response.getMsg());
        YlwebQydglWxtjrResponse wxtjrResponse=new YlwebQydglWxtjrResponse();
        BeanUtil.copyProperties(response,wxtjrResponse,false);
        return wxtjrResponse;
    }

}
